/*
 Source: ufxTrader.java
 Software Copyright: Hundsun Technologies Inc.
 System: UFX Security Demo (Asynchronous)
 Module: Main Function
 Describition:
 This class is the main class for security demo, the documents for function description and Java programming helper can be found at ufx 
 website: https://ufx.hscloud.cn/ufx/html/doc/index.html
 Function id include: 333002,333101,333017,333102,333104,332255,620001-12
 author: Kai Zhan
 Date:2016/03/29
 Others:
 This demo is a sample for using Hundsun UFX System, we are highly not recommand any user to use this demo at any real market，for more details 
 please read <T2SDK JAVA Developer Manual>
 */
package demoMain;

import com.hundsun.t2sdk.impl.client.T2Services;
import demo.ufx.md.ufxMD;
import demo.ufx.trade.tradeCallBack;
import java.util.Scanner;
import demo.ufx.trade.ufxTrade;

public class Main {

    private T2Services server = T2Services.getInstance();//T2实例化
    private static String global_user_token = null;
    private static String global_branch_no = null;
    private static String global_client_id = null;
    private static String global_sysnode_id = null;

    public static void main(String[] args) {
        try {
            Main app = new Main();
            app.InitT2(); //初始化t2并且启动t2
            ufxTrade TraderObj = new ufxTrade();
            ufxMD MDObj = new ufxMD();
            TraderObj.Init();
            tradeCallBack tCBack = new tradeCallBack();
            MDObj.Init();
            System.out.println("Warnning !!! This demo is only for testing,not recommand to use for real market!!!");
            System.out.println("------------------------------------------------");
            System.out.println(" [1]  331100 请求登录			    ");
            System.out.println(" [0]  退出系统			    ");
            System.out.println("请输入数字然后按回车键进行操作选择：");
            String account = "941000323"; //资金账户70000173,70960128,70960172,70960020
            String password = "111111";//资金账户密码 111111
            int j = 0;
            Scanner in = new Scanner(System.in);
            String i = in.next();
            while (j == 0) {
                switch (i) {
                    case "1":
                        TraderObj.ReqFunction331100(account, password); //异步登录
                        Thread.sleep(300);//多线程任务处理过快，为获取登录后出参必须有等待时间
                        global_user_token = tCBack.user_token();//获取登录后返回的user_token
                        global_branch_no = tCBack.branch_no();//获取登录后返回的branch_no
                        global_client_id = tCBack.client_id();//获取登录后返回的client_id
                        global_sysnode_id = tCBack.sysnode_id(); //获取登录后返回的sysnode_id
                        //System.out.println("system node id is "+global_sysnode_id); //打印系统号
                        System.out.println(global_user_token); //打印user_token
                        //System.out.println(global_branch_no); //打印branch_no
                        //进入操作界面
                        menu(app, TraderObj, MDObj, account, password, global_branch_no, global_user_token, global_client_id, global_sysnode_id);//back to menu

                        break;
                    default:
                        System.exit(0);
                        break;
                }
            }

            while (true) {
                Thread.sleep(1000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //获取T2SDK配置文件中配置的连接 
    //启动T2SDK，只需要在服务器启动时调用一次就可以了
    public void InitT2() throws Exception {
        server.setT2sdkConfigString("config/t2sdk_hundsun-config.xml");// 配置文件路径
        server.init();
        server.start();

    }

    //menu 
    public static void menu(Main app, ufxTrade TraderObj, ufxMD MDObj, String account, String password, String branch_no, String user_token, String client_id, String sysnode_id) {
        try {
            System.out.println("Warnning !!! This demo is only for testing,not recommand to use this for real market!!!");
            System.out.println("------------------------------------------------");
            System.out.println(" [1]  333002 普通委托");
            System.out.println(" [2]  333101 证券委托查询");
            System.out.println(" [3]  333017 委托撤单");
            System.out.println(" [4]  333102 成交查询");
            System.out.println(" [5]  333104 持仓查询");
            System.out.println(" [6]  332255 资金查询");
            System.out.println(" [7]  620001-12 证券成交回报订阅");
            System.out.println(" [8]  620001-23 证券委托回报订阅");
            System.out.println("请输入数字然后按回车键进行操作选择：");
            //String op_entrust_way=null;
            String exchange_type = null;
            String entrust_no = null;
            String entrust_bs = null;
            String stock_code = null;
            String n = null;
            String entrust_prop = null;
            String entrust_price = null;
            String entrust_amount = null;
            Scanner in = new Scanner(System.in);
            String i = in.next();
            switch (i) {
                case "1"://普通委托
                    System.out.println("请输入证券代码，并按回车键确认");
                    stock_code = in.next();//证券代码输入
                    System.out.println("请输入数字选择交易类型：（0）资金 （1）上海 （2）深圳，并按回车键确认");
                    exchange_type = in.next();//交易所输入
                    System.out.println("请输入委托数量，并按回车键确认");
                    entrust_amount = in.next();//交易数量输入
                    System.out.println("请输入委托价格，并按回车键确认");
                    entrust_price = in.next();//交易价格输入
                    System.out.println("请输入数字选择买卖方向：（1）买入 （2）卖出，并按回车键确认");
                    entrust_bs = in.next();//买卖方向输入
                    System.out.println("请输入数字选择委托属性：（0）买卖 （1）配股 ...，并按回车键确认.");
                    entrust_prop = in.next();//委托属性输入
                    TraderObj.ReqFunction333002(user_token, exchange_type, "7", account, password, "2", stock_code, entrust_amount, entrust_price, entrust_bs, entrust_prop, sysnode_id);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);
                            break;
                    }
                    break;
                case "2"://证券委托查询
                    TraderObj.ReqFunction333101(client_id, branch_no, user_token, account, password, sysnode_id);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                    }
                    break;
                case "3"://委托撤单
                    System.out.println("请输入委托编号，并按回车键确认");
                    entrust_no = in.next();//需撤单委托编号输入
                    TraderObj.ReqFunction333017(user_token, branch_no, client_id, account, password, entrust_no, sysnode_id);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");//back to menu
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                    }
                    break;
                case "4"://成交查询
                    TraderObj.ReqFunction333102(user_token, branch_no, client_id, account, password, sysnode_id);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                    }
                    break;
                case "5"://持仓查询
                    TraderObj.ReqFunction333104(branch_no, client_id, user_token, account, password, sysnode_id);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                    }
                    break;
                case "6"://客户资金快速查询
                    TraderObj.ReqFunction332255(branch_no, client_id, user_token, account, password, sysnode_id);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                    }
                    break;
                case "7"://订阅-证券成交回报
                    MDObj.ReqFunction620001_12(account, branch_no);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                    }
                    break;
                case "8"://订阅-证券成交回报
                    MDObj.ReqFunction620001_23(account, branch_no);
                    Thread.sleep(1000);
                    System.out.println("1.返回主目录");
                    System.out.println("0.退出程序");
                    System.out.println("请输入数字然后按回车键进行操作选择：");
                    n = in.next();
                    switch (n) {
                        case "1":
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                        case "0":
                            System.exit(0);//end app
                            break;
                        default:
                            Thread.sleep(1000);
                            System.out.println("No such choice, back to the main menu");
                            menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                            break;
                    }
                    break;
                default:
                    Thread.sleep(1000);
                    System.out.println("No such choice, back to the main menu");
                    menu(app, TraderObj, MDObj, account, password, branch_no, user_token, client_id, sysnode_id);//back to menu
                    break;
            }

            while (true) {
                Thread.sleep(1000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
